package com.aisb.common;

import com.aisb.manage.AuthDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.apache.ibatis.ognl.DynamicSubscript.first;

@Service
public class CommonService {
    @Autowired
    CommonDao commonDao;

    @Autowired
    AuthDao authDao;

    public List navList(Map param) {
        List<Map> navFirstList = commonDao.navFirstList(param);
        List<Map> navSecondList = commonDao.navSecondList(param);

        Map firstMap = new HashMap();
        for (Map m : navFirstList) {
            firstMap.put(m.get("funcId"), m);
        }

        for (Map m : navSecondList) {
            String parentId = (String) m.get("parentId");
            Map first = (Map) firstMap.get(parentId);
            if(null == first){
                continue;
            }
            List subs = (List) first.get("subs");
            if (null == subs) {
                subs = new ArrayList();
            }
            subs.add(m);
            first.put("subs", subs);
        }

        return navFirstList;
    }

    public List areaSimpleAll() {
        return commonDao.areaAll();
    }

    public Map<String, Map> areaSimpleMap() {
        Map rm = new HashMap();

        List aList = commonDao.areaAll();
        if (null != aList) {
            for (Object m : aList) {
                rm.put(((Map) m).get("id"), m);
            }
        }

        return rm;
    }

    public Map<String, List> rolesForAllUser() {
        Map<String, List> rm = new HashMap();

        List<Map> rList = authDao.rolesForUser(null);

        if (null != rList) {
            for (Map m : rList) {
                String userId = (String) m.get("userId");
                List urList = rm.get(userId);
                if (null == urList) {
                    urList = new ArrayList();
                }
                urList.add(m);
                rm.put(userId, urList);
            }
        }

        return rm;
    }
}
